package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.CosDetailsCommentModel;
import com.zw.yzk.kabu.model.CosDetailsImageModel;
import com.zw.yzk.kabu.utils.CircleTransform;
import com.zw.yzk.kabu.utils.StringUtils;
import com.zw.yzk.kabu.view.activity.HomePageActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/15.
 */

public class CosDetailsCommentAdapter extends RecyclerView.Adapter<CosDetailsCommentAdapter.Holder> {

    private Context context;
    private List<CosDetailsCommentModel> dataList;

    public CosDetailsCommentAdapter(List<CosDetailsCommentModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<CosDetailsCommentModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }

        notifyDataSetChanged();
    }

    public void add(List<CosDetailsCommentModel> list) {
        if (list != null) {
            int start = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    public List<CosDetailsCommentModel> getDataList() {
        return dataList;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View rootView = LayoutInflater.from(context).inflate(R.layout.recycler_item_cosplay_details_comment, parent, false);
        return new Holder(rootView);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final CosDetailsCommentModel model = dataList.get(position);
        holder.name.setText(model.getShowName());
        holder.time.setText(model.getTime());

        if (model.isReply()) {
            int start = 2;
            int end = model.getComment().indexOf(":");
            holder.comment.setText(StringUtils.clickableText(
                    model.getComment(), start, end, new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            startUserPage(model.getReplyId());
                        }

                        @Override
                        public void updateDrawState(TextPaint ds) {
                            ds.setColor(ContextCompat.getColor(context, R.color.gold));
                            ds.setUnderlineText(false);    //去除超链接的下划线
                        }
                    }));
            holder.comment.setHighlightColor(ContextCompat.getColor(context, R.color.transparent));
            holder.comment.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            holder.comment.setText(model.getComment());
        }


        Glide.with(context)
                .load(model.getHead())
                .transform(new CircleTransform(context))
                .error(R.drawable.ic_default_head)
                .placeholder(R.drawable.ic_default_head)
                .into(holder.image);

        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserPage(model.getUserId());
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onCommentClicked(model);
            }
        });

        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserPage(model.getUserId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    /**
     * 打开他人主页
     *
     * @param userId useid
     */
    private void startUserPage(int userId) {
        Intent intent = new Intent(context, HomePageActivity.class);
        intent.putExtra(ActivityConstant.HOME_PAGE_ID, userId);
        context.startActivity(intent);
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.head)
        ImageView image;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.comment)
        TextView comment;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCommentClickedListener {
        void onCommentClicked(CosDetailsCommentModel model);
    }

    private OnCommentClickedListener listener;

    public void setOnCommentClickedListener(OnCommentClickedListener listener) {
        this.listener = listener;
    }
}
